package cn.itsource.org.controller;

import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = {"部门接口"})
@RestController  //@Controller(交给spring管理) + @ResponseBody(方法返回json格式)
@RequestMapping("/dept")
public class DepartmentController {

    /**
     * 注入业务层依赖
     */
    @Autowired
    private IDepartmentService service;

    /**
     * 选择上级部门
     */
    @GetMapping("/treeList")
    public JsonResult treeList(){
        //1.查询所有
        List<Department> list = service.treeList();
        //2.封装数据后返回
        return JsonResult.createSuccess(list);
    }

    /**
     * 查询所有部门
     */
    @GetMapping
    private JsonResult findAll(){
        //1.查询所有
        List<Department> departmentList = service.findAll();
        //2.封装对象后返回
        return JsonResult.createSuccess(departmentList);
    }

    /**
     * 查询一个部门
     */
    @GetMapping("/findOne/{id}")
    private JsonResult findOne(@PathVariable("id") Long id){
        Department serviceOne = service.findOne(id);
        return JsonResult.createSuccess(serviceOne);
        //可简写
//        return JsonResult.createSuccess(service.findOne(id));
    }

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    public PageList<Department> selectPage(@RequestBody DepartmentQuery query){
        return service.selectPage(query);
    }

    /**
     * 新增或修改的联合方法
     */
    @PostMapping
    @ApiOperation(value = "新增或修改部门",notes = "芒果")
    public JsonResult saveOrUpdate(@Valid @ApiParam(value = "新增或修改部门参数",required = true) @RequestBody Department department){

            service.saveOrUpdate(department);
            return JsonResult.creatSuccess();
    }

    /**
     * 根据id删除一个部门
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){

            service.deleteById(id);
            return JsonResult.creatSuccess();

    }

    /**
     * 批量删除
     */
//    @PostMapping("/deleteBatch")
//    public JsonResult batchDelete(@RequestBody DepartmentQuery query){
//
//            service.batchDelete(query.getIds());
//            return JsonResult.creatSuccess();
//
//    }

    /**
     * 查询上级部门
     */
    @PostMapping("/findParentDepts")
    public JsonResult findParentDepts(){
        return JsonResult.createSuccess(service.findParentDepts());
    }

}
